package com.suoluo.zenadmin.controllers.api.dest;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Post;

import com.suoluo.zenadmin.common.APPConstants;
import com.suoluo.zenadmin.common.StringUtil;
import com.suoluo.zenadmin.common.SystemType;
import com.suoluo.zenadmin.common.UBB;
import com.suoluo.zenadmin.controllers.JSONInterceptor.JSON;
import com.suoluo.zenadmin.service.InfoService;
import com.suoluo.zenadmin.service.app.AppService;
import com.suoluo.zenadmin.service.dest.DestService;
import com.suoluo.zenadmin.service.dest.DestTrafficService;
import com.suoluo.zenadmin.service.hotel.HotelService;
import com.suoluo.zenadmin.vo.BaseInfoVO;
import com.suoluo.zenadmin.vo.app.AppDestFeatureItemRespVO;
import com.suoluo.zenadmin.vo.app.AppDestFeatureItemTextRespVO;
import com.suoluo.zenadmin.vo.app.AppDestRespVO;
import com.suoluo.zenadmin.vo.app.AppDestTrafficRespVO;
import com.suoluo.zenadmin.vo.app.AppHotelRespVO;
import com.suoluo.zenadmin.vo.app.AppInstanceVO;
import com.suoluo.zenadmin.vo.dest.DestFeature2VO;
import com.suoluo.zenadmin.vo.dest.DestFeatureItem2VO;
import com.suoluo.zenadmin.vo.dest.DestTraffic2VO;
import com.suoluo.zenadmin.vo.dest.DestVO;
import com.suoluo.zenadmin.vo.hotel.Hotel2VO;
import com.suoluo.zenadmin.vo.hotel.HotelResource2VO;

@Path("/")
public class AppDestController {

	@Resource(name="appService")
	private AppService appService;
	
	@Resource(name="destService")
	private DestService destService;
	
	@Resource(name="destTrafficService")
	private DestTrafficService destTrafficService;
	
	@Resource(name = "infoService")
	private InfoService infoService;
	

	@Resource(name="hotelService")
	private HotelService hotelService;
	
	@JSON
	@Post("")
	public AppDestRespVO findDestInfo(@Param("destId")int destId,@Param("version")int version){
		
		AppDestRespVO destResp = new AppDestRespVO();
		destResp.setDestId(destId);
		//判断请求版本信息，如果版本相同，返回提示信息
		AppInstanceVO app = appService.findTokenByTypeAndDataId(APPConstants.DEST, destId);
		int destVersion = app.getVersion();
		//返回版本信息
		destResp.setVersion(destVersion);
		
		if(version == destVersion){
			
			//设置返回状态码和描述信息
			destResp.setResultCode(APPConstants.NO_UPGRADE);
			destResp.setResultMsg(APPConstants.NO_UPGRADE_MSG);
			
			return destResp;
		}
		//如果请求的版本大于当前库里的版本，提示版本参数不合法
		if(version > destVersion){
			destResp.setDestId(destId);
			//设置返回状态码和描述信息
			destResp.setResultCode(APPConstants.VERSION_ERROR);
			destResp.setResultMsg(APPConstants.VERSION_ERROR_MSG);
			
			return destResp;
		}
		
		//获取目的地信息
		DestVO dest = destService.findDestById(destId);
		dest.setContent(UBB.clearUBB(dest.getContent()));
		destResp.setDest(dest);
		//获取目的地下特色集合信息
		List<DestFeature2VO> features = destService.findDestFeatures(destId);
		for (DestFeature2VO feature : features) {
			feature.setContent(UBB.clearUBB(feature.getContent()));
		}
		destResp.setFeatures(features);
		
		//设置返回状态码和描述信息
		destResp.setResultCode(APPConstants.SUCCESS);
		destResp.setResultMsg(APPConstants.SUCCESS_MSG);
		
		return destResp;
	}
	@JSON
	@Post("traffic")
	public AppDestTrafficRespVO findDestTraffic(@Param("destId")int destId){
			AppDestTrafficRespVO resp = new AppDestTrafficRespVO();
			
			List<DestTraffic2VO> traffic = destTrafficService.findAllDestTrafficNoLevel(destId);
			resp.setTraffics(traffic);
			
			resp.setResultCode(APPConstants.SUCCESS);
			resp.setResultMsg(APPConstants.SUCCESS_MSG);
			
			return resp;
			
	}
	
	@JSON
	@Post("item")
	public AppDestFeatureItemRespVO findDestFeatureItemByFeatureId(@Param("featureIds")String features){
		AppDestFeatureItemRespVO resp = new AppDestFeatureItemRespVO();
		String[] param  = features.split(",");
		
		List<Integer> featureIds = new ArrayList<Integer>();
		for (String string : param) {
			featureIds.add(Integer.parseInt(string));
		}
		List<DestFeatureItem2VO> list = destService.findFeatureItems(featureIds);
//		for (DestFeatureItem2VO item : list) {
//			item.setContent(UBB.clearUBB(item.getContent()));
//		}
		
		resp.setItems(list);
		
		resp.setResultCode(APPConstants.SUCCESS);
		resp.setResultMsg(APPConstants.SUCCESS_MSG);
		
		return resp;
		
		
	}
	
	@JSON
	@Post("itemdetail")
	public AppDestFeatureItemTextRespVO findDestFeatureItemDetail(@Param("itemId")int itemId,@Param("featureIds")String featureIds){
		AppDestFeatureItemTextRespVO resp = new AppDestFeatureItemTextRespVO();
		//获取指定特色项的文本信息
		List<BaseInfoVO> list = infoService.findBaseInfos(itemId, SystemType.INFO_DEST_FEATURE_ITEM);
		String content = null;
		for (BaseInfoVO base : list) {
			if(content == null){
				content = UBB.clearUBB(base.getContent());
			}
			else
			{
				content = content + "\r\n" + UBB.clearUBB(base.getContent());
			}
		}
		resp.setContent(content);
		
		//判断特色id是否为null，不为空要查询特色的特色项集合
		if(!StringUtil.isEmpty(featureIds))
		{
			String[] param  = featureIds.split(",");
			
			List<Integer> featureId = new ArrayList<Integer>();
			for (String string : param) {
				featureId.add(Integer.parseInt(string));
			}
			List<DestFeatureItem2VO> items = destService.findFeatureItems(featureId);
			resp.setItems(items);
		}
		
		resp.setResultCode(APPConstants.SUCCESS);
		resp.setResultMsg(APPConstants.SUCCESS_MSG);
		
		return resp;
		
		
	}
	
	
	@JSON
	@Post("hotel")
	public AppHotelRespVO findHotelInfo(@Param("hotelId")int hotelId){
		AppHotelRespVO resp = new AppHotelRespVO();
		//查询酒店信息
		Hotel2VO hotel = hotelService.findHotelById(hotelId);
		hotel.setContent(UBB.clearUBB(hotel.getContent()));
		
		resp.setHotel(hotel);
		//查询酒店风格信息
		String styles = hotelService.findHotelStyle(hotelId);
		resp.setStyles(styles);
		
		//查询酒店资源信息
		List<HotelResource2VO> list = hotelService.findHotelResources(hotelId);
		resp.setResources(list);
		
		resp.setResultCode(APPConstants.SUCCESS);
		resp.setResultMsg(APPConstants.SUCCESS_MSG);
		
		return resp;
	}
}
